package demo.practice.dp;

public class P560 {

    public static void main(String[] args) {
        P560 p560 = new P560();
        p560.subarraySum(new int[]{1, 2, 3}, 3);
    }

    public int subarraySum(int[] nums, int k) {
        int[] store = new int[nums.length + 1];
        for (int i = 1; i < store.length; i++) {
            store[i] = store[i - 1] + nums[i - 1];
        }

        int c = 0;  //store[i]- store[j] 代表 i~j之间的和
        for (int i = 0; i < store.length; i++) {
            for (int j = i - 1; j >= 0; j--) {
                if (store[i] - store[j] == k) {
                    c++;
                } else if (store[i] - store[j] > k) {
                    break;
                }
            }
        }
        return c;
    }
}
